package com.ara.websocket;

import android.app.Notification;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.Nullable;

import com.ara.websocket.listener.WebSocketResultCallback;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by XieXin on 2021/7/5.
 * WebSocket 服务
 */
public class XWebSocketService extends Service {
    private static final String TAG = XWebSocketService.class.getSimpleName();
    private static XWebSocketHelper helper;
    private static final List<WebSocketResultCallback> callbacks = new ArrayList<>();
    /*** WebSocket Url */
    private static String WEB_SOCKET_URL;
    /*** 发送心跳数据 */
    private static String sendHeartBeatData;

    /**
     * 启动WebSocket服务
     *
     * @param context 上下文
     */
    public static void startWebSocketService(Context context, String serverUrl, String sendHeartBeatData) {
        XWebSocketService.WEB_SOCKET_URL = serverUrl;
        XWebSocketService.sendHeartBeatData = sendHeartBeatData;
        callbacks.clear();
        Intent intent = new Intent(context, XWebSocketService.class);
        context.startService(intent);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (TextUtils.isEmpty(WEB_SOCKET_URL)) return super.onStartCommand(intent, flags, startId);
        Log.i(TAG, "启动服务连接WebSocket");
        if (helper != null && !helper.isEmpty() && !helper.isClose()) helper.close();
        helper = null;
        helper = XWebSocketHelper.getInstance();
        helper.init(WEB_SOCKET_URL, sendHeartBeatData, callbacks);
        helper.connect();

        Notification.Builder builder = new Notification.Builder(this.getApplicationContext());
        builder.setContentTitle("前台服务")
                .setContentText("前台服务")
                .setWhen(System.currentTimeMillis());
        Notification notification = builder.build();
        startForeground(0, notification);

        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        XWebSocketService.clear();
        if (helper != null && !helper.isEmpty() && !helper.isClose()) helper.close();
        helper = null;
    }

    /**
     * 取消连接
     */
    public static void close(Context context) {
        if (helper != null) helper.close();
        Intent intent = new Intent(context, XWebSocketService.class);
        context.stopService(intent);
    }

    /**
     * 重连
     */
    public static void reconnection() {
        if (helper != null) helper.reconnection();
    }

    /**
     * WebSocket客户端是否为空 true-空
     */
    public static boolean isEmpty() {
        boolean isEmpty = true;
        if (helper != null) isEmpty = helper.isEmpty();
        Log.d(TAG, "=======> WebSocket isEmpty " + isEmpty);
        return isEmpty;
    }

    /**
     * WebSocket是否取消连接 true-是
     */
    public static boolean isClose() {
        boolean isClose = true;
        if (helper != null) isClose = helper.isClose();
        Log.d(TAG, "=======> WebSocket isClose " + isClose);
        return isClose;
    }

    /**
     * 发送消息
     *
     * @param message String
     */
    public static void send(String message) {
        if (TextUtils.isEmpty(message)) {
            Log.e(TAG, "Message sending cannot null ");
            return;
        }
        if (helper != null) helper.send(message);
    }

    /**
     * 添加监听
     */
    public static void addCallback(WebSocketResultCallback callback) {
        callbacks.add(callback);
    }

    /**
     * 删除监听
     */
    public static void removeCallback(WebSocketResultCallback callback) {
        callbacks.remove(callback);
    }

    /**
     * 清除所有监听
     */
    public static void clear() {
        callbacks.clear();
    }
}
